package com.ruoyi.cs.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.github.pagehelper.Constant;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.cs.domain.TBattleUser;
import com.ruoyi.cs.domain.TUser;
import com.ruoyi.cs.domain.TUserRank;
import com.ruoyi.cs.domain.vo.TUserRobotBattleInfoVo;
import com.ruoyi.cs.mapper.TBattleUserMapper;
import com.ruoyi.cs.mapper.TUserMapper;
import com.ruoyi.cs.service.ITBattleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 对战用户信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-08-05
 */
@Service
@SuppressWarnings("all")
public class TBattleUserServiceImpl implements ITBattleUserService
{
    @Autowired
    private TBattleUserMapper tBattleUserMapper;
    @Autowired
    private TUserMapper tUserMapper;

    /**
     * 查询对战用户信息
     * 
     * @param battleruserId 对战用户信息ID
     * @return 对战用户信息
     */
    @Override
    public TBattleUser selectTBattleUserById(String battleruserId)
    {
        return tBattleUserMapper.selectTBattleUserById(battleruserId);
    }

    /**
     * 查询对战用户信息列表
     * 
     * @param tBattleUser 对战用户信息
     * @return 对战用户信息
     */
    @Override
    public List<TBattleUser> selectTBattleUserList(TBattleUser tBattleUser)
    {
        return tBattleUserMapper.selectTBattleUserList(tBattleUser);
    }

    /**
     * 新增对战用户信息
     * 
     * @param tBattleUser 对战用户信息
     * @return 结果
     */
    @Override
    public int insertTBattleUser(TBattleUser tBattleUser)
    {
        tBattleUser.setCreateTime(DateUtils.getNowDate());
        return tBattleUserMapper.insertTBattleUser(tBattleUser);
    }

    /**
     * 修改对战用户信息
     * 
     * @param tBattleUser 对战用户信息
     * @return 结果
     */
    @Override
    public int updateTBattleUser(TBattleUser tBattleUser)
    {
        tBattleUser.setUpdateTime(DateUtils.getNowDate());
        return tBattleUserMapper.updateTBattleUser(tBattleUser);
    }

    /**
     * 批量删除对战用户信息
     * 
     * @param battleruserIds 需要删除的对战用户信息ID
     * @return 结果
     */
    @Override
    public int deleteTBattleUserByIds(String[] battleruserIds)
    {
        return tBattleUserMapper.deleteTBattleUserByIds(battleruserIds);
    }

    /**
     * 删除对战用户信息信息
     * 
     * @param battleruserId 对战用户信息ID
     * @return 结果
     */
    @Override
    public int deleteTBattleUserById(String battleruserId)
    {
        return tBattleUserMapper.deleteTBattleUserById(battleruserId);
    }

    @Override
    public List<TUserRobotBattleInfoVo> robatBattleInfoList() {
        return tBattleUserMapper.robatBattleInfoList();
    }

    /**
     * 对战排名
     * @param type
     * @param userId
     * @return
     */
    @Override
    public Map battleRanking(String type, String userId) {
        if(type.equalsIgnoreCase("today")){
            type = "0";
        }else {
            type = "1";
        }
        Map map = new HashMap();
        List<TUserRank> tUserRanks = tBattleUserMapper.battleRanking(type);
        TUserRank tUserRank = null;
        if (StringUtils.isNotEmpty(userId) && tUserRanks.size()>0){
            TUser tUser = tUserMapper.selectTUserById(userId);
            if(tUser.getUserType().equals(Constants.USERTYPE0)){
                /**判断当前用户*/
                List<TUserRank> tUserRanks1 = tUserRanks.stream().filter(bean -> bean.getUserId().equals(userId)).collect(Collectors.toList());
                if(tUserRanks1.size()>0){
                    tUserRank = tUserRanks1.get(0);
                    tUserRank.setUserId(null);
                }
            }
        }
        if(tUserRanks.size()>0){
            int num = tUserRanks.size()>=10 ? 10 : tUserRanks.size();
            tUserRanks = tUserRanks.subList(0,num);
            for (TUserRank userRank : tUserRanks) {
                userRank.setUserId(null);
            }
        }
        map.put("list",tUserRanks);
        map.put("user",tUserRank);
        return map;
    }
}
